package com.tuyrt.`as`.proj.main.fragment.home

import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.tuyrt.`as`.proj.common.ui.view.loadUrl
import com.tuyrt.`as`.proj.main.R
import com.tuyrt.`as`.proj.main.model.HomeBanner
import com.tuyrt.`as`.proj.main.route.HiRoute
import com.tuyrt.hi.library.util.AppGlobals
import com.tuyrt.hi.library.util.HiDisplayUtil
import com.tuyrt.hi.library.util.HiRes
import com.tuyrt.hi.ui.banner.core.HiBanner
import com.tuyrt.hi.ui.banner.core.HiBannerMo
import com.tuyrt.hi.ui.item.HiDataItem
import com.tuyrt.hi.ui.item.HiViewHolder


class BannerItem(val list: List<HomeBanner>) : HiDataItem<List<HomeBanner>, HiViewHolder>(list) {

    override fun onBindData(holder: HiViewHolder, position: Int) {
        val hiBanner = holder.itemView as HiBanner
        val modelList = mutableListOf<HiBannerMo>()
        list.forEach { homeBanner ->
            val hiBannerMo = object : HiBannerMo() {}
            hiBannerMo.url = homeBanner.cover
            modelList.add(hiBannerMo)
        }
        hiBanner.setOnBannerClickListener { viewHolder, bannerMo, position ->
            val bannerType = list[position].type
            if (TextUtils.equals(bannerType, HomeBanner.TYPE_GOODS)) {
                Toast.makeText(AppGlobals.get()!!, "点击进入banner详情", Toast.LENGTH_SHORT).show()
            } else {
                HiRoute.startActivity4Browser(list[position].url)
            }
        }
        hiBanner.setBannerData(modelList)
        hiBanner.setBindAdapter { viewHolder, mo, position ->
            (viewHolder.rootView as ImageView).loadUrl(mo.url)
        }
    }

    override fun getItemView(parent: ViewGroup): View? {
        val context = parent.context
        val banner = HiBanner(context)
        val layoutParams = RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, HiDisplayUtil.dp2px(160f))
        layoutParams.bottomMargin = HiDisplayUtil.dp2px(10f)
        banner.layoutParams = layoutParams
        banner.setBackgroundColor(HiRes.getColor(R.color.color_white))
        return banner
    }
}